package com.example.anxiang.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.anxiang.R;
import com.example.anxiang.activity.api.Api;
import com.example.anxiang.activity.api.ApiConfig;
import com.example.anxiang.activity.api.MsgCallback;
import com.example.anxiang.activity.entity.LoginResponse;
import com.example.anxiang.activity.util.AppConfig;
import com.example.anxiang.activity.util.StringUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * The type Login activity.
 */
public class LoginActivity extends BaseActivity {
    private EditText etAccount;
    private EditText etPassword;
    private Button btLogin;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        etAccount=findViewById(R.id.account);
        etPassword=findViewById(R.id.password);
        btLogin=findViewById(R.id.login);
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account=etAccount.getText().toString().trim();
                String password=etPassword.getText().toString().trim();
                login(account,password);
            }
        });
    }

    /**
     * 登录
     * @param account
     * @param password
     */
    private void login(String account, String password) {
        if (StringUtil.isEmpty(account)){
            showToast("请输入账号");
            return;
        }
        if (StringUtil.isEmpty(password)){
            showToast("请输入密码");
            return;
        }
        HashMap<String,Object> params= new HashMap<>();
        params.put("email",account);
        params.put("password",password);
        Api.config(ApiConfig.LOGIN,params).postRequest(new MsgCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess",res);
                Gson gson=new Gson();
                LoginResponse loginResponse=gson.fromJson(res,LoginResponse.class);
                if (loginResponse.getCode()==200){
                    String token=loginResponse.getToken();
                    saveToken("token",token);
                    navigateTo(MainActivity.class);
                    showToastSync("登录成功");
                }
                else {
                    showToastSync("登录失败");
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
